<template>
  <div class="login">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm" style="margin-top: 50px">
      <el-form-item label="用户名" prop="name">
        <el-input v-model="ruleForm.name" placeholder="请输入用户名"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="pwd">
        <el-input type="password" v-model="ruleForm.pwd" placeholder="请输入密码"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @keyup.13="onSubmit" @click="onSubmit" >登录</el-button>
        <el-button @click="reset">重置</el-button>
      </el-form-item>
    </el-form>
  </div>

</template>

<script>
export default {
  name: 'Login',
  data () {
    return {
      ruleForm: {
        name: '',
        pwd:'',
        resource: '',
        desc: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        pwd: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 12, message: '长度在 6 到 12 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods:{
    onSubmit() {
      let username = this.ruleForm.name;
      let password = this.ruleForm.pwd;
      if(username&&password){
        this.$shop.post('login',{username,password})
          .then(res=>{
            if(res.status=="200"){
              //保存token到session中
              window.sessionStorage.setItem('token',res.data.data.token);
              //跳转登录
              this.$router.push('/home')
              this.$message.success('登录成功')
            }else {
              this.$message.error('登录失败')
            }
          }).catch(res=>{
            this.$message.error('登录失败')
        })
      }else {
        return
      }
    },
    reset(){
      this.ruleForm.name = '',
      this.ruleForm.pwd = ''
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
body { background:#fff; color:#555; font-size:14px; font-family: Verdana, Arial, Helvetica, sans-serif; }
td,th,caption { font-size:14px; }
.login{
  margin: 0 auto;
  margin-top: 200px;
  width: 400px;
  height: 300px;
  border: 1px solid #555555;
  border-radius: 5px;
  padding: 30px;
}
</style>
